## 04/28/2017
## tsne analysis
##

library("tsne");
mycolors <- c(rainbow(6),"orange","thistle","darkcyan","darkgreen");
fullmat <- read.table(file="~/ish8_cut.csv",sep=",",header=T);
#fullmat <- read.table(file="~/ish8_nocut.csv",sep=",",header=T);
cellnum <- fullmat[,1];
fullmat <- fullmat[,2:10];

paramvals <- c(5,10,15,20,25,30,35,40,50);
mytsnes <- vector("list",length(paramvals));
mykmlist <- vector("list",10);

for (j in 1:length(paramvals)) {
param <- paramvals[j];
mytsnes[[j]] <- tsne(fullmat,k=2,initial_dims=param,perplexity=param,max_iter=1000);
}
for (i in 3:10) {mykmlist[[i]] <- kmeans(fullmat,center=i,iter.max=100,nstart=1000);}
# completed computation above, output results below

fmc_names <- colnames(fullmat);
rampGrnBlkRed <- colorRamp(colors=c("green","black","red"));

for (j in 1:length(paramvals)) {
param <- paramvals[j];

# Plot tSNE Color Points by Gene (Green-Black-Red)
for (ic in 1:dim(fullmat)[2]) {
fn <- paste("Rplot.tsne.fm.v",param,".",fmc_names[ic],".pdf",sep="");
pdf(fn,wid=6,height=6);
colvals <- rgb(rampGrnBlkRed((fullmat[,ic]-min(fullmat[,ic]))*(1/(max(fullmat[,ic])-min(fullmat[,ic])))),max=255);
plot(mytsnes[[j]],pch=16,col=colvals,xlab="t-SNE 1",ylab="t-SNE 2",main=paste("t-SNE Plot Colored by Intensity of",fmc_names[ic]),cex=.6);
legend("topleft",leg=fmc_names[ic],cex=.8);
dev.off();
}

for (i in 3:10) {
# Plot tSNE Color Points by KM-Membership
fn <- paste("Rplot.tsne.fm.v",param,".km",i,".pdf",sep="");
pdf(fn,wid=6,height=6);
colkey <- match(1:i,sort(mykmlist[[i]]$size,decr=T,index.ret=T)$ix);
plot(mytsnes[[j]],pch=16,col=mycolors[colkey][mykmlist[[i]]$cluster],xlab="t-SNE 1",ylab="t-SNE 2",main=paste("t-SNE plot with KM Clusters, k =",i),cex=.6);
legend("topleft",leg=c(1:i),fill=mycolors[colkey][1:i],cex=.7);
dev.off();

# Plot KM Centroids Heatmap with Colors
fn <- paste("Rplot.tsne.fm.v",param,".km",i,".cent.pdf",sep="");
pdf(fn,wid=6,height=6);
colkey <- match(1:i,sort(mykmlist[[i]]$size,decr=T,index.ret=T)$ix);
heatmap(as.matrix(mykmlist[[i]]$centers),RowSideColors = mycolors[colkey],col=gray(0:255/255),scale="none",main=paste("Heatmap of KM Cluster Centroids, i =",i),marg=c(10,10));
dev.off();

# Write out data csv files with cellnum + tsne + km info
fn <- paste("data.tsne.fm.v",param,".km",i,".csv",sep="");
write.table(file=fn,cbind(cellnum,mytsnes[[j]],mykmlist[[i]]$cluster), sep=",",qu=F,row.names=F,col.names=c("cellnum","tsne1","tsne2","km_cluster"));
}
}

